//
//  LoseCodeViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/31.
//  Copyright © 2017年 杨旭. All rights reserved.
//
#define NUM @"0123456789"
#import "LoseCodeViewController.h"
#import "LoseCodeView.h"
#import "UITextField+text.h"
#import "ConsumptionViewController.h"
#import "ScanningModel.h"
@interface LoseCodeViewController ()<UITextFieldDelegate,LoseCodeViewDelegate>
@property (nonatomic, strong) LoseCodeView *loseCodeView;
@property (nonatomic, strong) UITextField *securitiespwd;
@property (nonatomic, strong) UILabel *placeholderText;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation LoseCodeViewController

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

//- (void)setInterval:(NSNumber *)interval {
//     _interval = interval;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"重庆小面";
    
    [self createVerificationUI];
    [self createKeyboardUI];
}

- (void)loadDataInputCode {
    
//    118.190.98.64/tctxAPI/input_code.aspx
    NSString *urlStr = [NSString stringWithFormat:@"%@/input_code.aspx",serverUrl];
    NSDictionary *paramDic = @{@"keysecret":self.securitiespwd.text};
    [NetworkManager requestPOSTWithURLStr:urlStr paramDic:paramDic finish:^(id responseObject) {
        NSLog(@"请求成功%@",responseObject);
        NSDictionary *dict = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([[dict objectForKey:@"state"] integerValue] == 1) {
            NSDictionary *dict = [responseObject objectForKey:@"data"];
            ScanningModel *model = [ScanningModel mj_objectWithKeyValues:dict];
            [self.dataArr addObject:model];
            
            ConsumptionViewController *consumptionVC = [[ConsumptionViewController alloc] init];
            consumptionVC.model = model;
            consumptionVC.keySecret = self.securitiespwd.text;
            [self.navigationController pushViewController:consumptionVC animated:YES];
        }else if ([[dict objectForKey:@"state"] integerValue] == 0){
            NSLog(@"操作失败%@",dict);
            [self showAlertWith:responseObject[@"message"]];
        }
        
    } enError:^(NSError *error) {
        NSLog(@"请求失败%@",error);
    }];

}


- (void)createKeyboardUI {

    self.loseCodeView = [[LoseCodeView alloc] initWithFrame:(CGRectMake((size_width - (68*3 + 12*4))/2, 128, 68*3 + 12*4, size_height))];
    self.loseCodeView.backgroundColor = [UIColor whiteColor];
    self.loseCodeView.delegate = self;
    [self.view addSubview:self.loseCodeView];
    YXWeakSelf
    self.loseCodeView.buttonNumberClicked = ^(NSInteger index) {
        weakSelf.placeholderText.hidden = YES;
        
        [weakSelf.securitiespwd changetext:[NSString stringWithFormat:@"%ld",index]];
        NSLog(@"%@",weakSelf.securitiespwd.text);
        if (weakSelf.securitiespwd.text.length > 12) {
            [weakSelf showAlertWithTitle:@"超过最大数字不能输入了"];
    }

        
//        if(self.interval && (self.securitiespwd.text.length+1) % ([self.interval integerValue] + 1) == 0)
//            [weakSelf.securitiespwd insertText:@" "];
    };
}

- (void)createVerificationUI {
    
    UIView *verificationView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 104))];
    verificationView.backgroundColor = [UIColor colorWithHexString:@"#ff9900"];
    [self.view addSubview:verificationView];
    
    
    UIImageView *backView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shurukuang"]];
    backView.frame = CGRectMake((verificationView.frame.size.width - 280)/2, 14, 280, 60);
    [verificationView addSubview:backView];
    
    self.placeholderText = [UILabel setLabelWithText:@"请输入同城劵密码" fontSize:16 color:[UIColor whiteColor]];
    self.placeholderText.frame = backView.frame;
    self.placeholderText.textAlignment = NSTextAlignmentCenter;
    [verificationView addSubview:self.placeholderText];
    
    self.securitiespwd = [[UITextField alloc] init];
    _securitiespwd.frame = CGRectMake((verificationView.frame.size.width - 248)/2, 14, 248, 60);
//    securitiespwd.backgroundColor = [UIColor redColor];
    _securitiespwd.font = [UIFont systemFontOfSize:28.0];
    _securitiespwd.textAlignment = NSTextAlignmentLeft;
    _securitiespwd.textColor = [UIColor whiteColor];
    _securitiespwd.tintColor = [UIColor whiteColor];
    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_shanchu"]];
    _securitiespwd.rightView=image;
    _securitiespwd.rightViewMode = UITextFieldViewModeUnlessEditing;
    _securitiespwd.clearButtonMode = UITextFieldViewModeAlways;
    _securitiespwd.delegate = self;
    [verificationView addSubview:self.securitiespwd];
//    self.securitiespwd.ry_interval = 4;
 
}

#pragma mark - 验码按钮
- (void)checkCodeButton {

    [self loadDataInputCode];

}

#pragma mark - textField的代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    // 把文本输入框的内容置为空
    textField.text = nil;
    [textField resignFirstResponder];
    self.loseCodeView.keyboardBtn1.enabled = YES;
    self.loseCodeView.keyboardBtn2.enabled = YES;
    self.loseCodeView.keyboardBtn3.enabled = YES;
    self.loseCodeView.keyboardBtn4.enabled = YES;
    self.loseCodeView.keyboardBtn5.enabled = YES;
    self.loseCodeView.keyboardBtn6.enabled = YES;
    self.loseCodeView.keyboardBtn7.enabled = YES;
    self.loseCodeView.keyboardBtn8.enabled = YES;
    self.loseCodeView.keyboardBtn9.enabled = YES;
    self.loseCodeView.keyboardBtn0.enabled = YES;

    NSLog(@"将要开始编辑");
    return YES;
    
}

//开始编辑时触发，文本字段将成为first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.text = nil;
    self.placeholderText.hidden = YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    //返回一个BOOL值，指明是否允许在按下回车键时结束编辑
    //如果允许要调用resignFirstResponder 方法，这回导致结束编辑，而键盘会被收起
    [textField resignFirstResponder];
    //查一下resign这个单词的意思就明白这个方法了
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{  //string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反
    
    if ([string isEqualToString:@"\n"])  //按会车可以改变
    {
        return YES;
    }
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self.securitiespwd == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString length] > 12) { //如果输入框内容大于20则弹出警告
            textField.text = [toBeString substringToIndex:12];
            
            [self showAlertWith:@"超过最大数字不能输入了"];
            
            return NO;
        }
    }
    return YES;
    
    
}

@end
